	//import
	import model.UserModel;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import nrb.utils.XMLHelper;
	
	import view.cust_add_edit_user;
	import view.cust_view_user;
	
	//declare variables	
	[Bindable] private var collection_user_roles:ArrayCollection;
	[Bindable] private var collection_search_user_roles:ArrayCollection;
	[Bindable] private var xml_users:XML;
	private var helper_xml:XMLHelper;
	public var user_model:UserModel;
	public var add_edit_user_window:cust_add_edit_user;
	public var view_user_window:cust_view_user;
	private var pA:Object;

	
	//functions
	public function func_init(): void {		
		collection_user_roles = new ArrayCollection([{label:'Admin', data:'Admin'}, {label:'User', data:'User'}]);
		collection_search_user_roles = new ArrayCollection([{label:'All', data:''}, {label:'Admin', data:'Admin'}, {label:'User', data:'User'}]);
		xml_users = new XML();
		helper_xml = new XMLHelper();
		user_model = new UserModel();
		pA = new Object();
		
		pA = parentApplication;
		func_process_xml();
		
		//enable buttons
		btn_view.enabled = false;
		btn_edit.enabled = false;
		btn_delete.enabled = false;
	}
	
	private function func_process_xml(): void {
		//check if xml already exists, else create
		if (!helper_xml.func_check_fileexists(pA.FILENAME_USERS_XML)){
			var xml_parentnode:XML = XML("<users></users>");
			helper_xml.func_create_xml(xml_parentnode, pA.FILENAME_USERS_XML);
		}
		
		helper_xml.func_open_xml(pA.FILENAME_USERS_XML);
		xml_users = helper_xml.func_get_xml();
		
		//set provider in datagrid
		//datagrid_users.dataProvider = xml_users.user;
	}
	
	private function func_search(): void {
		datagrid_users.dataProvider = xml_users.user.(userid.indexOf(String(txt_userid.text)) >=0  && 
													  username.indexOf(String(txt_username.text)) >=0 && 
													  userrole.indexOf(String(cb_userrole.selectedItem.data)) >=0);
	}
	
	private function func_delete(): void {
		Alert.show("Are you sure you want to delete this user?", null,Alert.OK | Alert.CANCEL, this,func_delete_listener);
	}
	
	private function func_delete_listener(eventObj:CloseEvent): void {
		// Check to see if the OK button was pressed.
        if (eventObj.detail==Alert.OK) {
            if(xml_users.user[user_model.index].userid != pA.LOGGED_USER_ID){
				delete xml_users.user[user_model.index];
				helper_xml.func_write_xml(xml_users);
				datagrid_users.dataProvider = xml_users.user;
				//disable buttons
				func_set_buttons(false);
			}else{
				Alert.show('Cannot Delete Logged-in User');
			} 
        }

	}
	
	public function func_click_datagrid(event:Event): void {
		user_model = new UserModel();
		//set selected index
		user_model.index = event.currentTarget.selectedIndex;
		user_model.func_set_attributes(event.currentTarget.selectedItem);
		//enable buttons
		func_set_buttons(true);
	}
	
	private function func_new_user(): void {
		add_edit_user_window = cust_add_edit_user(PopUpManager.createPopUp(this, cust_add_edit_user, true));
        PopUpManager.centerPopUp(add_edit_user_window);
		add_edit_user_window.func_set_vars({xml_users:xml_users, helper_xml:helper_xml});
	}
	
	public function func_edit_user(): void {
		add_edit_user_window = cust_add_edit_user(PopUpManager.createPopUp(this, cust_add_edit_user, true));
        PopUpManager.centerPopUp(add_edit_user_window);
		add_edit_user_window.func_set_vars({xml_users:xml_users, helper_xml:helper_xml, user_model:user_model});
	}
	
	public function func_view_user(): void {
		view_user_window = cust_view_user(PopUpManager.createPopUp(this, cust_view_user, true));
        PopUpManager.centerPopUp(view_user_window);
		view_user_window.func_set_vars({user_model:user_model});
	}
	
	private function func_clear_form(): void {
		txt_userid.text = '';
		txt_username.text = '';
		cb_userrole.selectedIndex = 0;
	}
	
	private function func_reset_datagrid(): void {
		datagrid_users.dataProvider = null;
		func_set_buttons(false);
	}
	
    public function func_open_popup():void {
    	
    }
    
    public function func_set_buttons(is_enabled:Boolean	): void {
    	btn_view.enabled = is_enabled;
		btn_edit.enabled = is_enabled;
		btn_delete.enabled = is_enabled;
    }
	
	